﻿using System;
using System.Collections.Generic;
using System.Web;

namespace Citi.Import.Util
{
    public static class CacheFacade
    {
        public static IList<T> Get<T>(Func<IList<T>> getItemCallback) where T : class
        {
            var cacheId = string.Concat("__", typeof(T).Name);

            var item = HttpRuntime.Cache.Get(cacheId) as IList<T>;

            if (item == null)
            {
                item = getItemCallback();
                HttpRuntime.Cache.Insert(cacheId, item);
            }

            return item;
        }

        public static void Clear(Type entity)
        {
            var cacheId = string.Concat("__", entity.Name);
            HttpRuntime.Cache.Remove(cacheId);
        }

        public static IList<T> Refresh<T>(Func<IList<T>> getItemCallback) where T : class
        {
            Clear(typeof (T));
            return Get(getItemCallback);
        }
    }
}